/*************************************************************
 * Title: 
 * Description: 
 * Author: Snow Huang
 * Email: Shaobin.Software@gmail.com Shaobin_Software@163.com
 * CreateTime: 2010-7-11 下午04:43:26
 * Copyright © 落羽社区 All right reserved
 ************************************************************/
package snow.biz.system.web;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;

import snow.core.domain.action.BaseAction;
import snow.core.util.constants.ApplicationConstants;
import snow.core.util.toolkit.ImageHelper;

/**
 * 
 */
public class ValidateCodeAction extends BaseAction<Object> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 5225382375306556482L;

	private String formatName = "JPEG"; // 图片默认格式为JPEG

	private int validateCodeLength = 4; // 默认长度为4
	private int imageWidth = 80; // 图片默认宽度为80
	private int imageHeight = 20; // 图片默认高度为20

	private ByteArrayInputStream inputStream;

	/**
	 * @return
	 * @throws Exception
	 * @see com.opensymphony.xwork2.ActionSupport#execute()
	 */
	public String execute() throws Exception {
		String validCode = ImageHelper.getValidateCode(validateCodeLength);
		getSession().setAttribute(ApplicationConstants.SESSION_VALID_CODE, validCode);
		BufferedImage image = ImageHelper.getValidateCodeImageFrom(validCode, imageWidth, imageHeight);
		ByteArrayOutputStream output = new ByteArrayOutputStream();
		ImageOutputStream imageOut = ImageIO.createImageOutputStream(output);
		ImageIO.write(image, formatName, imageOut);
		imageOut.close();
		setInputStream(new ByteArrayInputStream(output.toByteArray()));

		return SUCCESS;
	}

	/**
	 * @return the formatName
	 */
	public String getFormatName() {
		return formatName;
	}

	/**
	 * @return the imageHeight
	 */
	public int getImageHeight() {
		return imageHeight;
	}

	/**
	 * @return the imageWidth
	 */
	public int getImageWidth() {
		return imageWidth;
	}

	/**
	 * @return the inputStream
	 */
	public ByteArrayInputStream getInputStream() {
		return inputStream;
	}

	/**
	 * @return the validateCodeLength
	 */
	public int getValidateCodeLength() {
		return validateCodeLength;
	}

	/**
	 * @param formatName
	 *            the formatName to set
	 */
	public void setFormatName(String formatName) {
		this.formatName = formatName;
	}

	/**
	 * @param imageHeight
	 *            the imageHeight to set
	 */
	public void setImageHeight(int imageHeight) {
		this.imageHeight = imageHeight;
	}

	/**
	 * @param imageWidth
	 *            the imageWidth to set
	 */
	public void setImageWidth(int imageWidth) {
		this.imageWidth = imageWidth;
	}

	/**
	 * @param inputStream
	 *            the inputStream to set
	 */
	public void setInputStream(ByteArrayInputStream inputStream) {
		this.inputStream = inputStream;
	}

	/**
	 * @param validateCodeLength
	 *            the validateCodeLength to set
	 */
	public void setValidateCodeLength(int validateCodeLength) {
		this.validateCodeLength = validateCodeLength;
	}

}
